<!DOCTYPE html>
<html>
{include file="public/head"}
<body>
<div class="">
	<form method="post" action="" class="layui-form layui-form-pane">
            <br />
		<div class="layui-form-item">
			<label class="layui-form-label">标题:</label>
			<div class="layui-input-block">
				<input type="text" name="title" class="layui-input">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">开始时间:</label>
			<div class="layui-input-block">
				<input type="text" name="start_time" class="layui-input" id="start_time">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">结束时间:</label>
			<div class="layui-input-block">
				<input type="text" name="end_time" class="layui-input" id="end_time">
			</div>
		</div>
		 <div class="layui-form-item">
                <label class="layui-form-label">封榜时间(选填):</label>
                <div class="layui-input-block">
                    <input type="text" name="seal_time" class="layui-input" id="seal_time" >
                </div>
            </div><div class="layui-form-item">
                <label class="layui-form-label">解榜时间(选填):</label>
                <div class="layui-input-block">
                    <input type="text" name="unmake_time" class="layui-input" id="unmake_time" >
                </div>
            </div>
		
		<div class="layui-form-item">
			<label class="layui-form-label">权限:</label>
			<div class="layui-input-block">
				<select name="private" lay-verify="required" id="sell" lay-filter="sel">
					<option value="0">公开</option>
					<option value="1">私有/密码</option>
				</select>
			</div>
		</div>

		<div class="layui-form-item layui-hide" id="pws">
			<label class="layui-form-label">密码:</label>
			<div class="layui-input-block">
				<input type="text" name="pws" class="layui-input">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">竞赛描述:</label>
			<textarea id="description" class="layui-hide"></textarea>
		</div>
                <div style="padding-bottom: 20px;" class="nyoj-clear-width">
                <div id='description-ed'></div> 
                </div>
            
		<div class="layui-form-item" pane>
			<label class="layui-form-label">是否可见:</label>
			<div class="layui-input-block">
				<input type="radio" name="defunct" value="N" title="是" checked>
				<input type="radio" name="defunct" value="Y" title="否">
			</div>
		</div>
		<div id="add_problem">
			<div class="layui-form-item">
				<div class="layui-btn layui-input-inline" onclick="add_problem()">
					<span class="layui-icon">&#xe608;</span>添加题目
				</div>
				<div class="layui-input-inline">
					<h1>ProblemId</h1>
				</div>
				<div class="layui-input-inline">
					<h1>Alias</h1>
				</div>
				<div class="layui-input-inline">
					<h1>Title</h1>
				</div>
			</div>
			<div class="layui-form-item" id="0">
				<div class="layui-btn layui-input-inline" onclick="del_problem(0)">
					<span class="layui-icon">&#xe640;</span>删除
				</div>
				<div class="layui-input-inline">
					<input type="text" class="layui-input add_input" id="problem_id0" onblur="get_title(0)">
				</div>
				<div class="layui-input-inline">
					<input type="text" class="layui-input add_input" id="alias0">
				</div>
				<div class="layui-input-inline">
					<input type="text" id="title0" class="layui-input del_span" readonly>
				</div>
			</div>
		</div>
		<div style="text-align: center;">
			<input type="button" name="" lay-submit value="添加" class="layui-btn" lay-filter="save">
		</div>
	</form>
</div>
{include file="public/end"}
<script>
	var num = 0;
	function get_title(id) {
		layui.use('jquery',function() {
			var $ = layui.jquery;
			var val = $('#problem_id'+id).val();
			$.ajax({
				type:'get',
				url: "{:url('admin/contest/get_title')}",
				data:{
					id : val
				},
				dataType:'json',
				success:function(data) {
					$('#title'+id).val(data.title);
				},
			});
		});
	}
	function del_problem(id) {
		layui.use('jquery',function() {
			var $ = layui.jquery;
			$("#"+id).remove();
		});
	}
	function add_problem() {
		layui.use('jquery',function(){
			num++;
			var $ = layui.jquery;
			$('#add_problem').append('<div class="layui-form-item" id="'+num+'">\
				<div class="layui-btn layui-input-inline" onclick="del_problem('+num+')">\
				<span class="layui-icon">&#xe640;</span>删除\
				</div>\
				<div class="layui-input-inline">\
				<input type="text" class="layui-input add_input" id="problem_id'+num+'" onblur="get_title('+num+')">\
				</div>\
				<div class="layui-input-inline">\
				<input type="text" class="layui-input add_input" id="alias'+num+'">\
				</div>\
				<div class="layui-input-inline">\
				<input type="text" id="title'+num+'" class="layui-input del_span" readonly>\
				</div>\
				</div>');
		});
	}
	layui.use(['form','laydate','jquery','layer'], function(){
		var form = layui.form;
		var laydate = layui.laydate;
		var $ = layui.jquery;
		var layer = layui.layer;
		
		laydate.render({ 
			elem: '#start_time'
			,type: 'datetime'
		});
		laydate.render({ 
			elem: '#end_time'
			,type: 'datetime'
		});
                laydate.render({ 
			elem: '#seal_time'
			,type: 'datetime'
		});
                laydate.render({ 
			elem: '#unmake_time'
			,type: 'datetime'
		});
		form.on('select(sel)', function(data){
			if(data.value == 1) {
				$("#pws").removeClass('layui-hide');
			} else {
				$('#pws').addClass('layui-hide');
			}
		});
		form.on('submit(save)',function(data){
			var problem = [];
			var cont = 0;
			for(var i=0; i<=num; i++) {
				var id = "#problem_id"+i;
				var alias = "#alias"+i;
				var title = "#title"+i;
				if($(id).val()) {
					problem.push({
						problem_id : $(id).val(),
						alias : $(alias).val(),
						problem_title : $(title).val(),
						num : cont
					});
					cont++;
				}
			}
			var datas = data.field;
			datas.problem = problem;
			var loading2 = layer.load();
			$.ajax({
				url:'{:url("admin/contest/add_opt")}',
				type:"POST",
				data:datas,
				dataType:"json",
				success:function(json){
					layer.close(loading2);
					alert('添加成功！');
					location.href="{:url('admin/contest/editor')}";
				},
				error:function(res) {
					layer.close(loading2);
					//alert("添加失败！");
					location.href="{:url('admin/contest/add')}";
				}
			});
			return false;
		});
	});
</script>
<script type="text/javascript" src="__STATIC__/WE/wangEditor.min.js"></script>
<script>
    
layui.use(['jquery'],function(){
    var $=layui.jquery;
    
    var E = window.wangEditor;  
        var editor = new E('#description-ed');
        editor.customConfig.uploadImgServer = '/api/uploadimage'; // 上传图片到服务器
        editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024;
        editor.customConfig.uploadFileName = 'file';
        var $text1 = $('#description');
        editor.customConfig.onchange = function (html) {
            // 监控变化，同步更新到 textarea
            $text1.val(html);
        };
        editor.customConfig.menus = [
        'head',  // 标题
    'bold',  // 粗体
    'fontSize',  // 字号
    'fontName',  // 字体
    'italic',  // 斜体
    'underline',  // 下划线
    'strikeThrough',  // 删除线
    'foreColor',  // 文字颜色
    'backColor',  // 背景颜色
    'link',  // 插入链接
    'list',  // 列表
    'justify',  // 对齐方式
    'quote',  // 引用
    'image',  // 插入图片
    'table',  // 表格
    'video',  // 插入视频
    'code',  // 插入代码
    'undo',  // 撤销
    'redo'  // 重复
    ];
        editor.create();
        $text1.val(editor.txt.html());
});
</script>
</html>